using System;
using System.Drawing;
using FireStarter.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using Habanero.Util;

namespace FireStarterModeller.UI.ViewEditor
{
    public class XmlUIViewsEditorControl : UserControlWin, IUIViewsEditor
    {
        public IControlFactory ControlFactory { get; private set; }

        public IUIViewsEditorControl UIViewsEditorControl { get; set; }
        public IUIViewSelectorControl UIViewSelectorControl { get; internal set;}
        public IUIXmlEditorControl UIXmlEditorControl { get; internal set; }
        public IFireStarterFormsController FormsController { get; private set; }

        public XmlUIViewsEditorControl(IControlFactory controlFactory, IFireStarterFormsController formsController)
        {
            ControlFactory = controlFactory;
            FormsController = formsController;
            
            //XmlTextEditor xmlTextEditor = new CrossThreadRunner().RunInSTA(() => new XmlTextEditor());
            //UIXmlEditorControl uiXMLEditorControl = new UIXmlEditorControl(ControlFactory,
            //                                                               new DMToClassDefsXmlConverter(),
            //                                                               xmlTextEditor);
            //ScintillaWinXmlEditor scintilla = new CrossThreadRunner().RunInSTA(() => new ScintillaWinXmlEditor());
            //ScintillaWinXmlEditor scintilla = new ScintillaWinXmlEditor();
            //textBox.Multiline = true;
            //textBox.ScrollBars = ScrollBars.Both;
            //((TextBoxWin) textBox).WordWrap = false;
            //textBox.Font = new Font("Courier new", 9, FontStyle.Regular);

            DMToClassDefsXmlConverter xmlConverter = new DMToClassDefsXmlConverter();
            UIXmlEditorControl = new UIXmlEditorControl(ControlFactory, xmlConverter, formsController.CreateXmlEditor());
            UIPreviewControl uiPreviewControl = new UIPreviewControl(ControlFactory);
            UIViewSelectorControl = new UIViewSelectorControl(ControlFactory, xmlConverter);
            UIViewSelectorControl.XMLCreatedToInsert += InsertXml;

            IConverter<DMSolution, ClassDefCol> converter = new DMToEntityClassDefsConverter();
            UIViewsEditorControl = new UIViewsEditorControl(ControlFactory, formsController, UIXmlEditorControl,
                                                                 uiPreviewControl, UIViewSelectorControl, converter);
            BorderLayoutManager layoutManager = ControlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(UIViewsEditorControl, BorderLayoutManager.Position.Centre);
            
            Enabled = false;
        }

        private void InsertXml(object sender, StringEventArgs e)
        {
            //Find cursor position and insert text into the middle depending on this cursor
            //Explicit cast is needed here in order to get the cursor position, since the editor property is too general as IControlHabanero.
            int currentPos = ((ScintillaWinXmlEditor) UIXmlEditorControl.UIViewXmlEditor).CurrentPos;
            string oldText = UIXmlEditorControl.UIViewXmlEditor.Text;
            UIXmlEditorControl.UIViewXmlEditor.Text = oldText.Substring(0, currentPos) + e.Text +
                                                      oldText.Substring(currentPos, oldText.Length - currentPos);
            ((ScintillaWinXmlEditor) UIXmlEditorControl.UIViewXmlEditor).CurrentPos = currentPos + e.Text.Length;
        }

        public void EditUIViews(DMClass dmClass)
        {
            this.Enabled = (dmClass != null);
            if (dmClass == null) return;
            this.UIViewsEditorControl.EditUIViews(dmClass.UIViews);
        }
    }
}